package com.zlq.cn.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.config.Config;
import com.zlq.cn.controller.BaseController;
import com.zlq.cn.entity.CityOpen;
import com.zlq.cn.service.un.CityOpenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "城市配置")
@RestController
@RequestMapping("/api/city/open")
public class CityOpenController extends BaseController {

    @Resource
    private CityOpenService cityOpenService;

    @ApiOperation("城市列表")
    @PostMapping("/list")
    public SignApiResult<Page<CityOpen>> list(Page<CityOpen> page, String cityName) {
        return SignApiResult.resultSuccess(cityOpenService.lambdaQuery()
                .like(StringUtils.isNotEmpty(cityName), CityOpen::getCityName, cityName)
                .page(page));
    }

    @ApiOperation("城市删除")
    @PostMapping("/delete")
    public SignApiResult<Boolean> delete(String cityCode) {
        cityOpenService.lambdaUpdate()
                .eq(CityOpen::getCityCode, cityCode)
                .eq(CityOpen::getProject, Config.PROJECT_AIZH)
                .remove();
        return SignApiResult.resultSuccess(true);
    }

    @ApiOperation("城市新增")
    @PostMapping("/save")
    public SignApiResult<Boolean> save(@Valid CityOpen cityOpen) {
        return SignApiResult.resultSuccess(cityOpenService.insert(cityOpen));
    }

    @ApiOperation("城市启用禁用")
    @PostMapping("/updateEnable")
    public SignApiResult<Boolean> updateEnable(String cityCode, Boolean open) {
        cityOpenService.lambdaUpdate()
                .set(CityOpen::getOpen, open)
                .eq(CityOpen::getCityCode, cityCode)
                .eq(CityOpen::getProject, Config.PROJECT_AIZH)
                .update();
        return SignApiResult.resultSuccess(true);
    }

}
